<template>
  <div :class="className" :style="{height:height,width:width}"></div>
</template>

<script>
import echarts from 'echarts'
import { debounce } from '@/utils'
require('echarts/theme/macarons') // echarts theme

const animationDuration = 2600

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '100%'
    }
  },
  data () {
    return {
      chart: null
    }
  },
  mounted () {
    this.initChart()
    this.__resizeHanlder = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHanlder)
  },
  beforeDestroy () {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHanlder)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart () {
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption({
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        calculable:true,
        series: [
            {
                name: '学科分布',
                type: 'pie',
                radius: [15, 50],
                center: ['50%', '50%'],
                roseType: 'area',
                data: [
                    { value: 320, name: '前端' },
                    { value: 240, name: 'java' },
                    { value: 149, name: 'python' },
                    { value: 100, name: 'c++' },
                    { value: 59, name: 'UI' }
                ],
                animationEasing: 'cubicInOut',
                animationDuration: animationDuration
            }
        ]
      })
    }
  }
}
</script>
